package com.sgu6.sgu6_machine.adapter;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sgu6.sgu6_machine.R;

import java.util.List;

public class LogAdapter extends RecyclerView.Adapter implements View.OnClickListener {

    private Context context;
    private List<String> logsName;

    public LogAdapter(Context context, List<String> logsName) {
        this.context = context;
        this.logsName = logsName;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(context).inflate(R.layout.item_log, viewGroup, false);
        v.setOnClickListener(this);
        return new Holder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        viewHolder.itemView.setTag(i);
        if (viewHolder instanceof Holder){
            ((Holder) viewHolder).tvLogName.setText(logsName.get(i));
        }
    }

    @Override
    public int getItemCount() {
        return logsName.size();
    }

    @Override
    public void onClick(View v) {
        if (listener!=null){
            int position = (int) v.getTag();
            listener.onItemLogClick(position);
        }
    }


    static class Holder extends RecyclerView.ViewHolder {
        TextView tvLogName;

        public Holder(@NonNull View itemView) {
            super(itemView);
            tvLogName = itemView.findViewById(R.id.log_name);
        }
    }


    public interface OnItemLogClickListener{
        void onItemLogClick(int position);
    }

    private OnItemLogClickListener listener;

    public void setOnItemLogClickListener(OnItemLogClickListener listener){
        this.listener = listener;
    }
}
